package com.yeskery.transfer.handler;

import com.yeskery.transfer.config.DataBaseProperties;
import com.yeskery.transfer.core.KeyList;
import com.yeskery.transfer.core.Resource;
import com.yeskery.transfer.core.Result;
import com.yeskery.transfer.data.ConnectionKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.ResultHandler;
import org.springframework.stereotype.Component;

/**
 * 数据库配置信息结果处理对象，包含服务器数据库配置信息和
 * 客户端数据库配置信息
 * @author shuangjiang.du
 * @date 2018-12-13 17:36
 * @version 1.0
 */
@Component
public class DataBasePropertiesResultHandler implements ResultHandler<DataBaseProperties> {

	private final ConnectionKit connectionKit;

	private final Resource resource;

	@Autowired
	public DataBasePropertiesResultHandler(ConnectionKit connectionKit, Resource resource) {
		this.connectionKit = connectionKit;
		this.resource = resource;
	}

	@Override
	public void handleResult(DataBaseProperties dataBaseProperties) {
		DataBaseProperties.TransferProperties serverTransferProperties = dataBaseProperties.getServerTransferProperties();
		DataBaseProperties.TransferProperties clientTransferProperties = dataBaseProperties.getClientTransferProperties();
		Result result = new Result();
		String content = result.createContentLine("Server Database Configuration") + "\n"
				+ result.createBlankLine() + "\n[Host:" + serverTransferProperties.getHost() + "]\n"
				+ "[Port:" + serverTransferProperties.getPort() +"]\n"
				+ "[Database:" + serverTransferProperties.getDatabase() + "]\n"
				+ "[Username:" + serverTransferProperties.getUsername() + "]\n"
				+ "[Password:" + serverTransferProperties.getPassword() + "]\n"
				+ result.createBlankLine();
		result.setTitle("Server Database Configuration");
		result.setContent(content);
		result.setTips("If settings incorrect, run again.");
		System.out.println(result.output());
		System.out.println();
		System.out.println(result.createContentLine("Test Connection"));
		System.out.println(result.createBlankLine());
		boolean success = connectionKit.testConnection(serverTransferProperties);
		System.out.println(success ? result.createContentLine("Success")
				: result.createContentLine("Failure, Check database configuration."));
		System.out.println(result.createBlankLine());
		System.out.println();
		resource.add(KeyList.SERVER_CONFIG_VALID, success);
		if (success) {
			content = result.createContentLine("Client Database Configuration") + "\n"
					+ result.createBlankLine() + "\n[Host:" + clientTransferProperties.getHost() + "]\n"
					+ "[Port:" + clientTransferProperties.getPort() +"]\n"
					+ "[Database:" + clientTransferProperties.getDatabase() + "]\n"
					+ "[Username:" + clientTransferProperties.getUsername() + "]\n"
					+ "[Password:" + clientTransferProperties.getPassword() + "]";
			result.setTitle("Client Database Configuration");
			result.setContent(content);
			result.setTips("If settings incorrect, run again.");
			System.out.println(result.output());
			System.out.println();
			System.out.println(result.createContentLine("Test Connection"));
			System.out.println(result.createBlankLine());
			success = connectionKit.testConnection(clientTransferProperties);
			System.out.println(success ? result.createContentLine("Success")
					: result.createContentLine("Failure, Check database configuration."));
			System.out.println(result.createBlankLine());
			resource.add(KeyList.CLIENT_CONFIG_VALID, success);
		}

	}
}
